package com.wyf.springbootmybatis.config;

import com.wyf.springbootmybatis.jobs.MyFirstJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 构建配置类
 *   创建"JobDetail"实现并定义"Trigger"注册到"scheduler"，启动"scheduler"开启调度
 */
@Configuration
public class QuartzConfig {

    // 准备JobDetail
    @Bean
    public JobDetail jobDetail01() {
        return JobBuilder.newJob(MyFirstJob.class).storeDurably().build();
    }

    @Bean
    // 准备Trigger
    public Trigger trigger01() {
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                // 每秒一次
                .withIntervalInSeconds(1)
                // 永久重复，一直执行下去
                .repeatForever();
        return TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .withSchedule(simpleScheduleBuilder)
                .forJob(jobDetail01())
                .build();
    }

    // 每5秒触发一次任务
    @Bean
    public Trigger trigger02() {
        return TriggerBuilder.newTrigger()
                .withIdentity("trigger2", "group1")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ? *"))
                .forJob(jobDetail01())
                .build();
    }
}
